<template>
  <div class="container">
    <div class="articles-container">
    <el-card class="box-card">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="学科名称">
          <el-row>
            <el-col :span="6" style="width: 200px">
              <el-input
                v-model="form.subjectName"
                size="small"
                style="width: 200px"
              ></el-input>
            </el-col>
            <el-col :span="3">
              <div class="two_whith">
                <el-button size="small" @click="delform">清空</el-button>
                <el-button type="primary" size="small" @click="searchBtn"
                >搜索</el-button
                >
              </div>
            </el-col>
            <el-col :span="12" style="text-align: right">
              <el-button
                type="success"
                icon="el-icon-edit"
                size="small"
                @click="isshowdel"
              >新增学科</el-button
              ></el-col
            >
          </el-row>
        </el-form-item>
      </el-form>
      <el-alert :title="`数据一共${this.page.counts}条`" type="info" show-icon>
      </el-alert>

<!--      表单区域-->
      <el-table :data="list" row-key="id" v-loading="loading">
        <el-table-column type="index" label="序号" />
        <el-table-column label="学科名称" prop="subjectName" />
        <el-table-column label="创建者" prop="username" />
        <el-table-column label="创建日期" prop="addDate" >
          <template v-slot="{row}">
            {{row.addDate | parseTimeByString }}
          </template>
        </el-table-column>
        <el-table-column label="前台是否显示" prop="isFrontDisplay" >
          <template v-slot="{row}">
            {{row.isFrontDisplay===1?'是':'否'}}
          </template>
        </el-table-column>
        <el-table-column label="二级目录" prop="twoLevelDirectory" />

        <el-table-column label="标签" prop="tags" />
        <el-table-column label="题目数量" prop="totals" />
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button
              type="text"
              size="small"
              @click="$router.push({path:'/subjects/directorys',query:{id:row.id,name:row.subjectName}})"
            >学科分类</el-button
            >
            <el-button
              type="text"
              size="small"
              @click="$router.push({path:'/subjects/tags',query:{id:row.id,name:row.subjectName}})"
            >学科标签</el-button
            >
            <el-button
              type="text"
              size="small"
              @click="amendsubject(row.id,row.subjectName,row.isFrontDisplay)"
            >修改</el-button
            >
            <el-button type="text" size="small" @click="delsubjectbtn(row.id)"
            >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-row style="margin-top: 20px; text-align: right" align="middle">
        <el-pagination
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.counts"
          :current-page.sync="page.page"
          :page-size.sync="page.pagesize"
          @current-change="getList"
          @size-change="getList"
        />
      </el-row>
    </el-card>
    <el-dialog
      width="400px"
      :title="showTitle"
      :visible="shownewly"
      @close="btnCancel"
    >
      <el-form
        ref="roleForm"
        :model="roleForm"
        :rules="rules"
        label-width="90px"
      >
        <el-form-item label="学科名称" prop="subjectName">
          <el-input
            v-model.trim="roleForm.subjectName"
            placeholder="请输入学科名称"
          />
        </el-form-item>
        <el-form-item label="是否显示">
          <el-switch
            v-model="roleForm.isFrontDisplay"
            active-value=1
            inactive-value=0
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="12">
          <el-button size="small" @click="btnCancel">取消</el-button>
          <el-button size="small" type="primary" @click="btnOK">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
      </div>
  </div>
</template>

<script>
//  :current-page.sync="page.page"
import { list, add, update, remove } from '@/api/hmmm/subjects.js'
export default {
  created () {
    this.getList()
  },
  components: {},
  data () {
    return {
      showTitle: '新增学科',
      loading: false,
      onglyid: null,
      showamendy: false,
      roleForm: {
        subjectName: '',
        isFrontDisplay: 1,
        id: null
      },
      rules: {
        subjectName: [{ required: true, message: '学科名称不能为空', trigger: 'blur' }]
      },
      shownewly: false,
      form: {
        subjectName: ''
      },
      list: [],
      page: {
        page: 1, // 当前页码
        pagesize: 10, // 每页请求多少条数据
        counts: 0 // 总数
      }
    }
  },
  methods: {
    // 搜索
    searchBtn () {
      this.page.page = 1
      this.getList()
    },
    // 获取详细书类表
    async getList () {
      this.loading = true
      const res = await list({
        ...this.page, ...this.form
      })
      console.log(res)
      this.loading = false
      this.list = res.data.items
      this.page.counts = res.data.counts
    },
    // 清空表单内容
    delform () {
      this.form = {
        subjectName: ''
      }
    },
    isshowdel () {
      this.showTitle = '新增学科'
      this.shownewly = true
    },
    btnCancel () {
      this.shownewly = false
      this.$refs.roleForm.resetFields()
      this.roleForm = {
        subjectName: '',
        isFrontDisplay: '1'
      }
    },
    // 添加学科
    async btnOK () {
      try {
        if (this.roleForm.id) {
          await this.$refs.roleForm.validate()
          await update(this.roleForm)
          this.$message.success('修改学科成功')
        } else {
          await this.$refs.roleForm.validate()
          await add(this.roleForm)
          this.$message.success('添加学科成功')
        }
        this.btnCancel()
        await this.getList()
      } catch (e) {
        console.log(e)
      }
    },
    amendsubject (id, subjectName, isFrontDisplay) {
      this.showTitle = '修改学科'
      console.log(isFrontDisplay)
      this.roleForm.isFrontDisplay = isFrontDisplay.toString()
      this.roleForm.subjectName = subjectName
      this.roleForm.id = id
      this.shownewly = true
    },
    btnCancell () {
      this.showamendy = false
    },
    // 是删除弹出层+删除学科
    async delsubjectbtn (id) {
      await this.$confirm('此操作将永久删除该学科, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      await remove({ id })
      this.$message.success('删除成功')
      await this.getList()
    }
  }
}
</script>

<style>
.articles-container{
  padding: 10px;
}
.box-card {
}
.button_left {
  height: 36px;
}
.two_whith {
  margin-left: 13px;
  width: 200px;
}
.span_right {
  position: relative;
  top: -40px;
}
.el-alert {
  margin-top: 0px;
}
.el-table {
  margin-top: 15px;
}
.el-switch {
  margin-left: 15px;
}
.modality {
  background: #fff;
}
</style>
